package com.huixuebao.api.controller.login;


import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.huixuebao.api.dto.InsuredOrderShortDTO;
import com.huixuebao.api.dto.UserDTO;
import com.huixuebao.api.service.rpc.InsureOrderService;
import com.huixuebao.api.util.RedisUtil;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * TODO
 * @author ft_yu
 * @version 1.0
 * @date 2020-11-16 22:04
 */
@Slf4j
@RestController()
@RequestMapping("/hzcx")
public class GrouInsureController {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    InsureOrderService insureOrderSerivce;

    @GetMapping("/group/{policyId}")
    public ApiResult<List<InsuredOrderShortDTO>> getInsureOrders(@PathVariable("policyId") String policyId) throws JsonProcessingException {
        String token = "token:" + policyId;
        //通过订单号将投保成功的订单写入到数据库
        UserDTO userDTO = JsonUtil.getObjectData(redisUtil.get(token).toString(), UserDTO.class);
        if (ObjectUtil.isNull(userDTO)) {
            ApiResult apiResult = new ApiResult();
            apiResult.setSuccess(false);
            apiResult.setMessage("请登录");
            return apiResult;
        }
        return insureOrderSerivce.listInsuredOrders(policyId);
    }
}
